package edu.guet.server.service.imple;

import edu.guet.server.mapper.UserInfoMapper;
import edu.guet.server.model.dto.UpdateUserInfoDTO;
import edu.guet.server.model.vo.UserInfoVO;
import edu.guet.server.service.UserInfoService;
import edu.guet.server.utils.AliOSSUtils;
import edu.guet.server.utils.ResultAPI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
@Slf4j
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper infoMapper;
    @Autowired
    private AliOSSUtils aliOSSUtils;

    /**
     * 根据用户id查信息
     * @param userId
     * @return
     */
    @Override
    public UserInfoVO getUserInfo(int userId) {
        return infoMapper.getStudentInfoById(userId);
    }

    /**
     * 修改用户基本信息
     * @param userInfoDTO
     * @return
     */
    @Override
    public ResultAPI updateUserInfo(UpdateUserInfoDTO userInfoDTO) {
        log.info("Updating user info: " + userInfoDTO);
        //String url = "https://mynoteapp-web.oss-cn-beijing.aliyuncs.com/f06cf4fc-0ff2-4e3a-b5ec-246fb6514034.png";
        infoMapper.updateUserInfo(userInfoDTO);
        return ResultAPI.success();
    }

    /**
     * 修改头像
     * @param file
     */
    @Override
    public ResultAPI updateAvatar(MultipartFile file, int user_id) {
        String url = null;
        try{
            if(file.isEmpty() || file == null){
                url = null;
            }else{
                url = aliOSSUtils.upload(file);
            }
        }catch (Exception e){
            return ResultAPI.error("上传失败！");
        }

        UpdateUserInfoDTO userInfoDTO = new UpdateUserInfoDTO();
        userInfoDTO.setImg_url(url);
        userInfoDTO.setUser_id(user_id);
        infoMapper.updateUserInfo(userInfoDTO);
        return ResultAPI.success(url, "修改成功");
    }


}
